//
//  ChoseSateView.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ChoseSateView.h"
#import "SateCollectionViewCell.h"
static NSString * CellIdentifer = @"SateCollectionViewCell";
@interface ChoseSateView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UIScrollView * scroView;
@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)void(^choseBlock)(NSMutableArray * array);
@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,assign)NSInteger sectionNumber;
@property(nonatomic,assign)NSInteger rowNumber;
@property(nonatomic,strong)NSMutableArray * choseSateArray;
@property(nonatomic,strong)NSArray * sateArray;
@property(nonatomic,strong)UIView * chosenSeatView;
@property(nonatomic,strong)UIView * leftView;
@end

@implementation ChoseSateView

- (instancetype)initWithFrame:(CGRect)frame Title:(NSString *)title SateLayoutArray:(NSArray * )sateLayoutArray complect:(void(^)(NSMutableArray * chosenArray))choseBlock{
    self = [super initWithFrame:frame];
    if (self) {
        _sectionNumber = sateLayoutArray.count;
        NSInteger row = 0;
        for (NSArray * array in sateLayoutArray) {
            if (array.count>row) {
                row = array.count;
            }
        }
        self.backgroundColor = [UIColor colorWithWhite:0.918 alpha:1.000];
        _rowNumber = row;
        _choseSateArray = [NSMutableArray array];
        _choseBlock = choseBlock;
        _sateArray = sateLayoutArray;
        
        
        _scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 37*VerticalRatio(), frame.size.width, frame.size.height-70*VerticalRatio()-37*VerticalRatio())];
       
        _scroView.maximumZoomScale = 1.5;
        _scroView.minimumZoomScale = 1.0;
        _scroView.showsHorizontalScrollIndicator = NO;
        _scroView.showsVerticalScrollIndicator = NO;
        _scroView.delegate = self;
        [self addSubview:_scroView];
        
        _backView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 20*_rowNumber, 18*(_sectionNumber+2)), NO)];
        _backView.backgroundColor = [UIColor clearColor];
        [_scroView addSubview:_backView];
        
        _scroView.contentSize = CGSizeMake(CGRectGetWidth(_backView.frame)+10, CGRectGetHeight(_backView.frame)+10);
       
        UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        self.collectionView = [[UICollectionView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 18, _rowNumber*20, _sectionNumber*18) , YES) collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor =[UIColor clearColor];
        self.collectionView.userInteractionEnabled=YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.collectionView registerNib:[UINib nibWithNibName:CellIdentifer bundle:nil] forCellWithReuseIdentifier:CellIdentifer];
        [_backView addSubview:_collectionView];
        
        
        
        
        UIView * leftView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(13, 9, 17, (1+_sectionNumber)*18), NO)];
        leftView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.403];
        leftView.layer.cornerRadius = 2*VerticalRatio();
        leftView.userInteractionEnabled = NO;
        [_backView addSubview:leftView];
        _leftView = leftView;
        
        
        for (int i = 1; i<=_sectionNumber; i++) {
            UILabel * label = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, 9+18*(i-1), 17, 18), YES)];
            label.text = [NSString stringWithFormat:@"%d",i];
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:11*VerticalTexeRatio()];
            [leftView addSubview:label];
        }
        
        
        UIView * leftLine = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(15, 30, 100, 1), YES)];
        leftLine.backgroundColor = [UIColor colorWithWhite:0.882 alpha:1.000];
        [self addSubview:leftLine];
    
        
        UIView * rightLine = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(205, 30, 100, 1), YES)];
        rightLine.backgroundColor = [UIColor colorWithWhite:0.882 alpha:1.000];
        [self addSubview:rightLine];
        
        UILabel * label = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, 22, 320, 15), YES)];
        label.text = title;
        label.textColor = [UIColor colorWithWhite:0.486 alpha:1.000];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12*VerticalTexeRatio()];
        [self addSubview:label];
        
        _chosenSeatView = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 285, 320, 70), YES)];
        _chosenSeatView.backgroundColor = [UIColor whiteColor];
        _chosenSeatView.hidden = YES;
        _chosenSeatView.alpha = 0;
        [self addSubview:_chosenSeatView];
        
        
        UILabel * chosenLabel = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(14, 12, 200, 15), YES)];
        chosenLabel.text = @"已选座位";
        chosenLabel.textColor = [UIColor colorWithWhite:0.200 alpha:1.000];
        chosenLabel.textAlignment = NSTextAlignmentLeft;
        chosenLabel.font = [UIFont systemFontOfSize:12*VerticalTexeRatio()];
        [_chosenSeatView addSubview:chosenLabel];
        
        UIView * upLine = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 1), YES)];
        upLine.backgroundColor = [UIColor colorWithWhite:0.945 alpha:1.000];
        [_chosenSeatView addSubview:upLine];
        
        UIView * downLine = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 69,320, 1), YES)];
        downLine.backgroundColor = [UIColor colorWithWhite:0.945 alpha:1.000];
        [_chosenSeatView addSubview:downLine];

    }
    
    return self;
}


- (void)deleteSeat:(UIButton *)sender{
    SateCollectionViewCell * cell = (SateCollectionViewCell *)[_collectionView cellForItemAtIndexPath:_choseSateArray[sender.tag-100]];
    cell.sateButton.selected = NO;
    [_choseSateArray removeObjectAtIndex:sender.tag - 100];
    _choseBlock(_choseSateArray);
    for (UIView * view in _chosenSeatView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    if (_choseSateArray.count==0) {
        _chosenSeatView.hidden = YES;
        _chosenSeatView.alpha = 0;
    }else{
        _chosenSeatView.hidden = NO;
        _chosenSeatView.alpha = 1;
        for (int i = 0;i<_choseSateArray.count;i++) {
            NSIndexPath * path = _choseSateArray[i];
            
            
            UIButton * label = [UIButton buttonWithType:UIButtonTypeCustom];
            label.frame = flexibleFrame(CGRectMake(6+76*i, 32, 70, 23), YES);
            [label setTitleColor:[UIColor colorWithWhite:0.376 alpha:1.000] forState:UIControlStateNormal];
            [label setTitle: [NSString stringWithFormat:@"%ld排%ld座",path.section+1,path.row+1] forState:UIControlStateNormal];
            label.titleLabel.font = [UIFont systemFontOfSize:13*VerticalTexeRatio()];
            [_chosenSeatView addSubview:label];
            
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = flexibleFrame(CGRectMake(12+76*i, 32, 70, 23), YES);
            button.backgroundColor = [UIColor clearColor];
            button.layer.borderWidth = 1;
            button.layer.cornerRadius = 3*VerticalRatio();
            button.layer.borderColor = [UIColor colorWithWhite:0.376 alpha:1.000].CGColor;
            [_chosenSeatView addSubview:button];
            
            UIButton * image = [UIButton buttonWithType:UIButtonTypeCustom];
            image.frame = flexibleFrame(CGRectMake(56, 8, 7, 7), YES);
            [image  setImage:[UIImage imageNamed:@"choseSeat4"] forState:UIControlStateNormal];
            [button addSubview:image];
            
            UIButton * aheadButton = [UIButton buttonWithType:UIButtonTypeCustom];
            aheadButton.backgroundColor = [UIColor clearColor];
            aheadButton.frame = flexibleFrame(CGRectMake(6+76*i, 26, 82, 35), YES);
            aheadButton.tag = 100+i;
            [aheadButton addTarget:self action:@selector(deleteSeat:) forControlEvents:UIControlEventTouchUpInside];
            [_chosenSeatView addSubview:aheadButton];


            
        }
    }

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _backView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _leftView.frame = flexibleFrame(CGRectMake(13+scrollView.contentOffset.x/(scrollView.zoomScale*VerticalRatio()), 9, 17, (1+_sectionNumber)*18), NO);
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
        return _rowNumber;

}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _sectionNumber;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

        return CGSizeMake(20*VerticalRatio(), 18*VerticalRatio());
        

    
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{

        return UIEdgeInsetsMake(0,0, 0, 0);
 
    
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
        SateCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifer forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
    
    NSInteger state = [_sateArray[indexPath.section][indexPath.row]integerValue];
    if (state == 0) {
        cell.sateButton.hidden = YES;
    }else if (state == 1){
        cell.sateButton.hidden = NO;
    }else if (state == 2){
        cell.sateButton.hidden = NO;
        [cell.sateButton setBackgroundImage:[UIImage imageNamed:@"choseSeat3"] forState:UIControlStateNormal];
        cell.sateButton.enabled = NO;

    }
    
   
    cell.choseSeateBlack = ^(NSInteger index){
        if (index == 1) {  if (_choseSateArray.count>=4) {
            UIAlertView * alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"最多选四个座位" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alterView show];
            SateCollectionViewCell * sate = (SateCollectionViewCell*)[_collectionView cellForItemAtIndexPath:indexPath];
            sate.sateButton.selected = NO;
        }else{
         [_choseSateArray addObject:indexPath];
        }
        }else{
            [_choseSateArray removeObject:indexPath];
        }
        _choseBlock(_choseSateArray);
               for (UIView * view in _chosenSeatView.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                [view removeFromSuperview];
            }
        }
        
        if (_choseSateArray.count==0) {
            _chosenSeatView.hidden = YES;
            _chosenSeatView.alpha = 0;
        }else{
            _chosenSeatView.hidden = NO;
            _chosenSeatView.alpha = 1;
            for (int i = 0;i<_choseSateArray.count;i++) {
                NSIndexPath * path = _choseSateArray[i];
                
                
                UIButton * label = [UIButton buttonWithType:UIButtonTypeCustom];
                label.frame = flexibleFrame(CGRectMake(6+76*i, 32, 70, 23), YES);
                [label setTitleColor:[UIColor colorWithWhite:0.376 alpha:1.000] forState:UIControlStateNormal];
               [label setTitle: [NSString stringWithFormat:@"%ld排%ld座",path.section+1,path.row+1] forState:UIControlStateNormal];
                label.titleLabel.font = [UIFont systemFontOfSize:13*VerticalTexeRatio()];
                [_chosenSeatView addSubview:label];
                
                UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = flexibleFrame(CGRectMake(12+76*i, 32, 70, 23), YES);
                button.backgroundColor = [UIColor clearColor];
                button.layer.borderWidth = 1;
                button.layer.cornerRadius = 3*VerticalRatio();
                button.layer.borderColor = [UIColor colorWithWhite:0.376 alpha:1.000].CGColor;
                [_chosenSeatView addSubview:button];
                
               
                
                UIButton * image = [UIButton buttonWithType:UIButtonTypeCustom];
                image.frame = flexibleFrame(CGRectMake(56, 8, 7, 7), YES);
                [image  setImage:[UIImage imageNamed:@"choseSeat4"] forState:UIControlStateNormal];
                [button addSubview:image];
                
                
                
                UIButton * aheadButton = [UIButton buttonWithType:UIButtonTypeCustom];
                aheadButton.backgroundColor = [UIColor clearColor];
                aheadButton.frame = flexibleFrame(CGRectMake(6+76*i, 26, 82, 35), YES);
                aheadButton.tag = 100+i;
                [aheadButton addTarget:self action:@selector(deleteSeat:) forControlEvents:UIControlEventTouchUpInside];
                [_chosenSeatView addSubview:aheadButton];
                
        }
    }
        
    };
    
    return cell;
    
}


@end
